package com.jerry.constellation.Utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.jerry.constellation.bean.StarInfoBean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AssetsUtils {

    private static Map<String, Bitmap> logoImgMap;
    private static Map<String, Bitmap> contentlogimgMap;

    /*
     * TODO:读取assets文件夹下的内容，存放入字符串
     *   */
    public static String getJsonfromAssets(Context context, String filename) {
        //1.获取Asset文件夹的管理器
        AssetManager assets = context.getResources().getAssets();
        //2.将读取到的内容统一保管
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //2.1 获取一个文件输入流
        try {
            InputStream inputStream = assets.open(filename);
            //读取内容，存放到内存中
            int hasRead = 0;
            byte[] buf = new byte[1024];
            while (true) {
                hasRead = inputStream.read(buf);
                if (hasRead == -1) {
                    break;
                }
                byteArrayOutputStream.write(buf, 0, hasRead);
            }
            String msg = byteArrayOutputStream.toString();
            inputStream.close();
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*TODO:读取assets文件夹下的图片，返回Bitmap对象
     * */
    public static Bitmap getBitmapFromAssets(Context context, String filename) {
        Bitmap bitmap = null;
        //1.获取图片管理者
        AssetManager assets = context.getResources().getAssets();
        try {
            InputStream in = assets.open(filename);
            //通过位图管理器，将输入流转换成位图对象
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    /*
     *@des 将assets文件夹下的图片一起读取，放置在内存中
     * */
    public static void saveBitmapFromAssets(Context context, StarInfoBean starInfoBean) {
        logoImgMap = new HashMap<>();
        contentlogimgMap = new HashMap<>();
        List<StarInfoBean.StarinfoBean> starlist = starInfoBean.getStarinfo();
        for (int i = 0; i < starlist.size(); i++) {
            String logoname = starlist.get(i).getLogoname();
            String filename = "xzlogo/" + logoname + ".png";
            Bitmap logoBitmap = getBitmapFromAssets(context, filename);
            logoImgMap.put(logoname, logoBitmap);

            String contentname = "xzcontentlogo/" + logoname + ".png";
            Bitmap bitmap = getBitmapFromAssets(context, contentname);
            contentlogimgMap.put(logoname, bitmap);

        }
    }

    public static Map<String, Bitmap> getContentlogimgMap() {
        return contentlogimgMap;
    }

    public static Map<String, Bitmap> getLogoImgMap() {
        return logoImgMap;
    }
}
